package edu.itstudy.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.itstudy.bean.User;
import edu.itstudy.services.IUserServices;
import edu.itstudy.services.impl.IUserServicesImpl;

public class UserUpdateServlet extends HttpServlet {
	
	private IUserServices iuserServices = new IUserServicesImpl();
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获取用户修改信息
		 * 把信息交给services帮忙修改
		 * services把修改结果告诉servlet
		 * services要DAO帮忙修改，DAO告诉services插入是否成功
		 * DAO通过JDBC连接数据库操作
		 * 根据services修改结果响应用户		
		 * true 回用户管理首页
		 * false 修改失败
		 */
		//设置字符编码
		request.setCharacterEncoding("UTF-8");
		
		String uid = request.getParameter("uid");
		String age = request.getParameter("age");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String intro = request.getParameter("intro");
		String recommend = request.getParameter("recommend");
		
		User user = new User(Integer.parseInt(uid),null,null,Integer.parseInt(age),null,phone,address,null,intro,Integer.parseInt(recommend),0);
		
		
		
		
		boolean result = iuserServices.updateUser(user);
		
		//业务流程控制
		if(result) {
			response.sendRedirect("userindex");
		}else {
			request.setAttribute("info","用户修改失败！");
			request.getRequestDispatcher("/WEB-INF/content/userUpdate.jsp").forward(request, response);
		}
	}
}
